#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution1
{
public:
    void solve()
    {
        int n; cin >> n;
        if (n == 1)
        {
            cout << 1 << endl;
            return;
        }

        vector<int> nums(n);
        for (int i = 0; i < n; ++i)
            cin >> nums[i];

        int res = 1;
        int i = 1; int flag = 0;
        while (i < n&& nums[i] == nums[i - 1]) ++i;
        flag = nums[i] > nums[i - 1] ? 1 : -1;
        ++i;
        while (i < n)
        {
            if (nums[i] > nums[i - 1])
            {
                if (flag == -1)
                {
                    ++res; ++i;
                    while (i < n&& nums[i] == nums[i - 1]) ++i;
                    if (i == n) break;
                    flag = nums[i] > nums[i - 1] ? 1 : -1;
                }
            }
            else if (nums[i] < nums[i - 1])
            {
                if (flag == 1)
                {
                    ++res; ++i;
                    while (i < n&& nums[i] == nums[i - 1]) ++i;
                    if (i == n) break;
                    flag = nums[i] > nums[i - 1] ? 1 : -1;
                }
            }
            else
            {
                while (i < n && nums[i] == nums[i - 1]) ++i;
                --i;
            }
            ++i;
        }
        cout << res << endl;
    }
};

class Solution2 {
public:
    int lengthOfLIS(vector<int>& nums) {
        int n = nums.size();
        vector<int> v;

        for (int i = 0; i < n; ++i)
        {
            if (v.empty() || v.back() < nums[i])
            {
                v.push_back(nums[i]);
            }
            else
            {
                int left = 0; int right = v.size() - 1;
                while (left < right)
                {
                    int mid = (right - left) / 2 + left;
                    if (v[mid] < nums[i])
                        left = mid + 1;
                    else
                        right = mid;
                }
                v[left] = nums[i];
            }
        }

        return v.size();
    }
};

class Solution3 {
public:
    using pii = pair<int, int>;

    int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
        int n = difficulty.size();
        vector<pii> dfps;
        for (int i = 0; i < n; ++i)
            dfps.push_back({ difficulty[i], profit[i] });
        sort(dfps.begin(), dfps.end());
        int mx = dfps[0].second;
        for (int i = 1; i < n; ++i)
        {
            dfps[i].second = max(mx, dfps[i].second);
            mx = max(mx, dfps[i].second);
        }
        int m = worker.size(); int res = 0;
        for (int i = 0; i < m; ++i)
        {
            int l = 0, r = n - 1;
            while (l < r)
            {
                int mid = (l + r + 1) >> 1;
                if (dfps[mid].first > worker[i]) r = mid - 1;
                else l = mid;
            }
            if (dfps[l].first <= worker[i])
                res += dfps[l].second;
        }
        return res;
    }
};

int main()
{
    vector<int> d{ 2,4,6,8,10 }, p{ 10,20,30,40,50 }, w{ 4,5,6,7 };
    Solution3().maxProfitAssignment(d, p, w);
    return 0;
}